#!/usr/bin/env perl
use strict;
use warnings;
use Test::More tests => 6;
use lib 'tests/search';
use AIR2TestUtils;
use Data::Dump qw( dump );
use JSON::XS;
use AIR2::SearchServer;

SKIP: {

    if ( !AIR2TestUtils::search_env_ok() ) {
        skip "The search env does not look sane. Skipping all tests", 6; 
    }
    my $idx_dir = AIR2::Config->get_search_index();
    if ( !-d $idx_dir ) {
        skip "Index dir $idx_dir is not a directory on this system", 6;
    }

    ok( my $at  = AIR2TestUtils->new_auth_tkt(), "get auth tkt object" );
    ok( my $tkt = AIR2TestUtils->dummy_tkt(),    "get dummy auth tkt" );
    ok( my $req = AIR2TestUtils::new_http_request(
            HTTP::Request->new(
                GET => "http://localhost:3002/?q=test&air2_tkt=$tkt"
            )
        ),
        "new HTTP::Request"
    );
    ok( my $resp = AIR2::SearchServer::handler( $at, $req ),
        "SearchServer::handler" );

    #dump($resp);

    ok( my $json = decode_json( $resp->content ),
        "json decode body of response" );

    #dump($json);

    cmp_ok( $json->{total}, '>', 3000, "total for q=test" );

}

